package org.shj.activitidemo.service;

import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Shen Huang Jian
 * @date 2019/9/17
 */
@Service
public class LeaveServiceImpl implements LeaveService {

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private ProcessEngine processEngine;

    public void startProcess(String userId, int days){
        /*Map<String, Object> params = new HashMap<>();
        params.put("days", days);
        params.put("empName", userId);
        params.put("staff", userId);
        params.put("formId", userId + System.currentTimeMillis());*/

        //启动流程实例，字符串"vacation"是BPMN模型文件里process元素的id
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave");
//流程实例启动后，流程会跳转到请假申请节点
        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
//设置请假申请任务的执行人
        taskService.setAssignee(task.getId(), userId);
        //taskService.claim();

        //设置流程参数：请假天数和表单ID
//流程引擎会根据请假天数days>3判断流程走向
//formId是用来将流程数据和表单数据关联起来
        Map<String, Object> params = new HashMap<>();
        params.put("days", days);
        params.put("empName", userId);
//        params.put("staff", userId);
//        params.put("formId", userId + System.currentTimeMillis());


        //完成请假申请任务
        taskService.complete(task.getId(), params);
    }
}
